var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/Upload/Upload.tsx
var Upload_exports = {};
__export(Upload_exports, {
  default: () => Upload_default
});
module.exports = __toCommonJS(Upload_exports);
var import_react = __toESM(require("react"));
var import_icons = require("@ant-design/icons");
var import_antd = require("antd");
var import_classnames = __toESM(require("classnames"));
var import_utils = require("@szzj/utils");
var import_style = require("antd/es/upload/style/index.js");
var import_style2 = require("antd/es/button/style/index.js");
var import_style3 = require("antd/es/message/style/index.js");
var import_createField = __toESM(require("../createField"));
var import_FieldContext = require("../_utils/contexts/FieldContext");
var import_utils2 = require("./utils");
var import_index = require("./index.less");
var { Dragger } = import_antd.Upload;
var DefaultErrorMessages = {
  success: "上传成功",
  error: "上传失败。${errorMsg}",
  overSize: "文件不能大于 ${maxSize}MB",
  typeError: "文件格式不正确"
};
var Upload = (0, import_react.forwardRef)(
  ({
    fileName,
    /** 作为value */
    fileList,
    onChange,
    beforeUpload: beforeUploadProp,
    children,
    draggable,
    className,
    accept: acceptProp,
    maxSize,
    errorMessages: errorMessagesProp,
    autoFilterErrorFile = true,
    ...rest
  }, ref) => {
    const { name } = (0, import_FieldContext.useFieldContext)() ?? {};
    const accept = (0, import_react.useMemo)(() => {
      return acceptProp ? (0, import_utils2.convetAliasToExtension)(acceptProp) : void 0;
    }, [acceptProp]);
    const errorMessages = (0, import_react.useMemo)(() => {
      return {
        ...DefaultErrorMessages,
        ...errorMessagesProp
      };
    }, [errorMessagesProp]);
    const validateFile = (0, import_react.useMemo)(() => {
      return (0, import_utils2.createFileValidator)({
        accept,
        maxSize,
        errorMessages
      });
    }, [accept, maxSize, errorMessages]);
    const beforeUpload = (0, import_react.useCallback)(
      (file, files) => {
        let errorMessage = void 0;
        try {
          validateFile(file);
        } catch (error) {
          errorMessage = error.message;
        }
        if (errorMessage) {
          import_antd.message.error(errorMessage);
          return import_antd.Upload.LIST_IGNORE;
        }
        if (beforeUploadProp)
          return beforeUploadProp(file, files);
      },
      [validateFile, beforeUploadProp]
    );
    const handleChange = (0, import_react.useCallback)(
      (info) => {
        if (info.file.status === "done") {
          if (info.file.response && info.file.response.success) {
            const msg = (0, import_utils.template)(errorMessages == null ? void 0 : errorMessages.success)(info.file.response);
            import_antd.message.success(msg);
          } else {
            const msg = (0, import_utils.template)(errorMessages == null ? void 0 : errorMessages.error)(
              info.file.response && typeof info.file.response === "object" ? info.file.response : { errorMsg: "" }
            );
            import_antd.message.error(msg);
          }
        } else if (info.file.status === "error") {
          const msg = (0, import_utils.template)(errorMessages == null ? void 0 : errorMessages.error)(
            info.file.response && typeof info.file.response === "object" ? info.file.response : { errorMsg: "" }
          );
          import_antd.message.error(msg);
        }
        if (autoFilterErrorFile) {
          info.fileList = info.fileList.filter((file) => {
            if (file.status === "error" || file.status === "done" && file.response && !file.response.success)
              return false;
            return true;
          });
        }
        onChange == null ? void 0 : onChange(info);
      },
      [errorMessages, onChange, autoFilterErrorFile]
    );
    const innerProps = {
      name: fileName ?? name,
      fileList,
      ref,
      accept,
      beforeUpload,
      onChange: handleChange
    };
    if (draggable) {
      return /* @__PURE__ */ import_react.default.createElement(
        Dragger,
        {
          ...rest,
          className: (0, import_classnames.default)(
            "szzj-upload",
            "szzj-upload-dragable",
            className
          ),
          style: { padding: "20px" },
          ...innerProps
        },
        children ? children : /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("p", { className: "ant-upload-drag-icon" }, /* @__PURE__ */ import_react.default.createElement(import_icons.InboxOutlined, null)), /* @__PURE__ */ import_react.default.createElement("p", { className: "ant-upload-text" }, "点击或拖拽上传文件"), accept && /* @__PURE__ */ import_react.default.createElement("p", { className: "ant-upload-hint" }, `支持的文件类型为：${accept}`))
      );
    }
    return /* @__PURE__ */ import_react.default.createElement(
      import_antd.Upload,
      {
        ...rest,
        className: (0, import_classnames.default)("szzj-upload", className),
        ...innerProps
      },
      children ? children : rest.listType === "picture-card" ? /* @__PURE__ */ import_react.default.createElement("div", { className: "szzj-upload-picture" }, /* @__PURE__ */ import_react.default.createElement(import_icons.PlusOutlined, { className: "szzj-upload-picture-icon" }), /* @__PURE__ */ import_react.default.createElement("div", { className: "ant-upload-text" }, "上传图片")) : /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { type: "default" }, /* @__PURE__ */ import_react.default.createElement(import_icons.UploadOutlined, { className: "szzj-upload-icon" }), "文件上传")
    );
  }
);
var Upload_default = (0, import_createField.default)(Upload, {
  defaultDetailType: "tag",
  defaultFormItemProps: {
    valuePropName: "fileList",
    /**
     * @todo 自动提取 response 响应数据，可添加 thumbUrl
     */
    getValueFromEvent: ({ fileList }) => {
      return fileList;
    },
    rules: [
      {
        validator: async (_, value) => {
          if (value.some((file) => file.status === "uploading")) {
            throw new Error("请等待文件上传完成");
          }
          if (value.some((file) => file.status === "error")) {
            throw new Error("请删除上传失败的文件");
          }
        }
      }
    ]
  }
});
